package controller

import (
	"hft/Database/admin"
	"hft/Http/Models/request"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"

	"github.com/gin-gonic/gin"
)

type CommonTag struct{}

func (*CommonTag) CreateTag(c *gin.Context) {
	var tag *admin.CommonTag

	c.ShouldBindJSON(&tag)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.TagServices.CreateTag(claims.BaseClaims, tag)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

func (*CommonTag) EditTag(c *gin.Context) {
	var tag *admin.CommonTag

	c.ShouldBindJSON(&tag)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.TagServices.EditTag(claims.BaseClaims, tag)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

func (*CommonTag) GetTagList(c *gin.Context) {
	var paginate request.CommonTagSearch

	c.ShouldBind(&paginate)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	list, err := services.TagServices.GetTagList(claims.BaseClaims, &paginate)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.OkWithData(list, c)
}

var TagController = new(CommonTag)
